package com.lft01.thread;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Function: 		ADD FUNCTION.
 * Reason:   		ADD REASON.
 * Date             2020/6/6 14:18
 * <p>
 * Class Name:      Restaurant
 * Package Name:    com.lft.thread
 *
 * @author Laifengting
 * @version 1.0.0
 * @since JDK 8
 */
public class Restaurant {
	private String status = "cut";
	Lock lock = new ReentrantLock();
	final Condition cut = lock.newCondition();
	final Condition cook = lock.newCondition();
	final Condition giving = lock.newCondition();
	
	public void cut() {
		lock.lock();
		try {
			while (!status.equals("cut")) {
				try {
					cut.await();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			System.out.println(Thread.currentThread().getName() + "  我先切菜");
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName() + "  30分钟过去了...");
			System.out.println(Thread.currentThread().getName() + "  我切完菜了");
			status = "cook";
			cook.signalAll();
		} finally {
			lock.unlock();
		}
	}
	
	public void cook() {
		lock.lock();
		try {
			while (!status.equals("cook")) {
				try {
					cut.await();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			System.out.println(Thread.currentThread().getName() + "  我来炒菜");
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName() + "  10分钟过去了...");
			System.out.println(Thread.currentThread().getName() + "  我炒完菜了");
			status = "giving";
			giving.signalAll();
		} finally {
			lock.unlock();
		}
	}
	
	public void giving() {
		lock.lock();
		try {
			while (!status.equals("giving")) {
				try {
					cut.await();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			System.out.println(Thread.currentThread().getName() + "  我来端菜菜");
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName() + "  1分钟过去了...");
			System.out.println(Thread.currentThread().getName() + "  我端完菜了");
			status = "cut";
			cut.signalAll();
		} finally {
			lock.unlock();
		}
	}
}
